﻿using System;

using ZQNB.Common;
using ZQNB.Common.Web;

namespace ZQNB.Web.Common.Helpers
{
    public class RouteResolver
    {
        public static string CurrentRouteInfo()
        {
            NbMvcRequest nbMvcRequest = MvcHelper.MakeRequest();
            string url = "";
            if (!string.IsNullOrWhiteSpace(nbMvcRequest.Site))
            {
                const string urlFormat = "{{Domain}}/{{Site}}/{{Area}}/{{Controller}}/{{Action}}, {QueryString:{QueryString}}, {IdentityName:{IdentityName}}";
                url = urlFormat
                    .Replace("{Domain}", nbMvcRequest.DomainUri)
                    .Replace("{Site}", nbMvcRequest.Site)
                    .Replace("{Area}", nbMvcRequest.Area)
                    .Replace("{Owner}", nbMvcRequest.Owner)
                    .Replace("{Controller}", nbMvcRequest.Controller)
                    .Replace("{Action}", nbMvcRequest.Action)
                    .Replace("{QueryString}", nbMvcRequest.QueryString)
                    .Replace("{IdentityName}", nbMvcRequest.IdentityName);
            }
            
            else if ("space".NbEquals(nbMvcRequest.Area) && !string.IsNullOrWhiteSpace(nbMvcRequest.Owner))
            {
                const string urlFormat = "{{Domain}}/Space/{{Owner}}/{{Controller}}/{{Action}}, {QueryString:{QueryString}}, {IdentityName:{IdentityName}}";
                url = urlFormat
                    .Replace("{Domain}", nbMvcRequest.DomainUri)
                    .Replace("{Site}", nbMvcRequest.Site)
                    .Replace("{Area}", nbMvcRequest.Area)
                    .Replace("{Owner}", nbMvcRequest.Owner)
                    .Replace("{Controller}", nbMvcRequest.Controller)
                    .Replace("{Action}", nbMvcRequest.Action)
                    .Replace("{QueryString}", nbMvcRequest.QueryString)
                    .Replace("{IdentityName}", nbMvcRequest.IdentityName);
            }

            else
            {
                const string urlFormat = "{{Domain}}/{{Area}}/{{Controller}}/{{Action}}, {QueryString:{QueryString}}, {IdentityName:{IdentityName}}";
                url = urlFormat
                    .Replace("{Domain}", nbMvcRequest.DomainUri)
                    .Replace("{Site}", nbMvcRequest.Site)
                    .Replace("{Area}", nbMvcRequest.Area)
                    .Replace("{Owner}", nbMvcRequest.Owner)
                    .Replace("{Controller}", nbMvcRequest.Controller)
                    .Replace("{Action}", nbMvcRequest.Action)
                    .Replace("{QueryString}", nbMvcRequest.QueryString)
                    .Replace("{IdentityName}", nbMvcRequest.IdentityName);
            }

            return url;
        }

        public static string MakeSiteUrl(string site, string area, string controller, string action, string queryString)
        {
            AssertNotEmpty("site", site);
            AssertNotEmpty("area", area);
            AssertNotEmpty("controller", controller);
            AssertNotEmpty("action", action);

            const string urlFormat = "/{site}/{area}/{controller}/{action}";
            string url = urlFormat
                .Replace("{site}", site)
                .Replace("{area}", area)
                .Replace("{controller}", controller)
                .Replace("{action}", action);

            url = AppendQueryString(url, queryString);
            return url.ToLower();
        }

        public static string MakeSpaceUrl(string owner, string controller, string action, string queryString)
        {
            AssertNotEmpty("owner", owner);
            AssertNotEmpty("controller", controller);
            AssertNotEmpty("action", action);

            const string urlFormat = "/space/{owner}/{controller}/{action}";
            string url = urlFormat
                .Replace("{owner}", owner)
                .Replace("{controller}", controller)
                .Replace("{action}", action);

            url = AppendQueryString(url, queryString);
            return url.ToLower();
        }

        public static string MakeUrl(string area, string controller, string action, string queryString)
        {
            AssertNotEmpty("area", area);
            AssertNotEmpty("controller", controller);
            AssertNotEmpty("action", action);

            const string urlFormat = "/{area}/{controller}/{action}";
            string url = urlFormat
                .Replace("{area}", area)
                .Replace("{controller}", controller)
                .Replace("{action}", action);

            url = AppendQueryString(url, queryString);
            return url.ToLower();
        }

        private static string AppendQueryString(string url, string queryString)
        {
            if (string.IsNullOrWhiteSpace(queryString))
            {
                return url;
            }

            url = url + "?" + queryString.TrimStart('?');
            return url;
        }
        private static void AssertNotEmpty(string name, string value)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                throw new ArgumentNullException(name, "参数不能为空");
            }
        }
    }
}